package com.webinsky.folder.persist.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.webinsky.common.utils.db.PageHelper;

public class FolderSqlProvider {
	
	private static final String SELECT_UNION_TEMPLATE = " select id, name, type, site_id, sort from ( "
						+ " select id, name, 0 as type, sort, site_id from item %s  "
						+ " union all "
						+ " select id, name, 1 as type, sort, site_id from catalog %s "
						+ " ) as result order by type asc, sort asc ";
	
	
	private static final String SELECT_UNION_COUNT_TEMPLATE = " select count(*) from ( "
			+ " select id, name, 0 as type, site_id from item %s "
			+ " union all "
			+ " select id, name, 1 as type, site_id from catalog %s "
			+ " ) as result ";
	
	public String selectFolderByNameAndDel(Map<String, Object> param){
		String sql = SELECT_UNION_TEMPLATE;
		String name = (String)param.get("name");
		String lang = (String)param.get("language");
		Integer del = (Integer)param.get("del");
		Integer pageSize = (Integer)param.get("pageSize");
		Integer pageStart = (Integer)param.get("pageStart");
		List<String> conditions = new ArrayList<>();
		if ( name != null ){
			conditions.add(" name like '%" + name + "%'");
		}
		if ( del != null ){
			conditions.add(" del = " + del);
		}
		if ( lang != null ){
			conditions.add(" language = '" + lang + "'");
		}
		String strCondition = "";
		if ( !conditions.isEmpty() ){
			strCondition = " where " + String.join(" and ", conditions);
		}
		sql = String.format(sql, strCondition, strCondition);
		if ( pageSize != null && pageStart != null && pageStart >=0 && pageSize > 0 ){
			return PageHelper.startPage(sql, pageStart, pageSize);
		}
		return sql;
	}
	
	public String selectCountByNameAndDel(Map<String, Object> param){
		String sql = SELECT_UNION_COUNT_TEMPLATE;
		String name = (String)param.get("name");
		Integer del = (Integer)param.get("del");
		String lang = (String)param.get("language");
		List<String> conditions = new ArrayList<>();
		if ( name != null ){
			conditions.add(" name like '%" + name + "%'");
		}
		if ( del != null ){
			conditions.add(" del = " + del);
		}
		if ( lang != null ){
			conditions.add(" language = '" + lang + "'");
		}
		String strCondition = "";
		if ( !conditions.isEmpty() ){
			strCondition = " where " + String.join(" and ", conditions);
		}
		return String.format(sql, strCondition, strCondition);
	}
	
	public String selectViewByBookNameAndDel(Map<String, Object> param) {
		String sql = SELECT_UNION_TEMPLATE;
		String name = (String)param.get("name");
		String lang = (String)param.get("language");
		Integer siteId = (Integer)param.get("siteId");
		Integer del = (Integer)param.get("del");
		Integer pageSize = (Integer)param.get("pageSize");
		Integer pageStart = (Integer)param.get("pageStart");
		List<String> conditions = new ArrayList<>();
		if ( name != null ){
			conditions.add(" name like '%" + name + "%'");
		}
		if ( del != null ){
			conditions.add(" del = " + del);
		}
		if ( siteId != null ) {
			conditions.add(" site_id = " + siteId);
		}
		if ( lang != null ){
			conditions.add(" language = '" + lang + "'");
		}
		String strCondition = "";
		if ( !conditions.isEmpty() ){
			strCondition = " where " + String.join(" and ", conditions);
		}
		sql = String.format(sql, strCondition, strCondition);
		if ( pageSize != null && pageStart != null && pageStart >=0 && pageSize > 0 ){
			return PageHelper.startPage(sql, pageStart, pageSize);
		}
		return sql;
	}
	
	public String selectCountByBookNameAndDel(Map<String, Object> param){
		String sql = SELECT_UNION_COUNT_TEMPLATE;
		String name = (String)param.get("name");
		Integer siteId = (Integer)param.get("siteId");
		Integer del = (Integer)param.get("del");
		String lang = (String)param.get("language");
		List<String> conditions = new ArrayList<>();
		if ( name != null ){
			conditions.add(" name like '%" + name + "%'");
		}
		if ( del != null ){
			conditions.add(" del = " + del);
		}
		if ( siteId != null ) {
			conditions.add(" site_id = " + siteId);
		}
		if ( lang != null ){
			conditions.add(" language = '" + lang + "'");
		}
		String strCondition = "";
		if ( !conditions.isEmpty() ){
			strCondition = " where " + String.join(" and ", conditions);
		}
		return String.format(sql, strCondition, strCondition);
	}	
}
